package postpic.app;

import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import org.json.JSONException;
import org.json.JSONObject;

import postpic.app.io.ClientIO;
import postpic.domain.action.Action;
import postpic.domain.session.Request;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class PostPicActivity extends Activity implements OnClickListener {
	
	private static final int CAMERA_PIC_REQUEST = 1337;
	private static final int SELECT_PHOTO = 1338;
	private Bitmap photo;
	
	
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    
    /** Handle the clicking of buttons */
    public void onClick(View v) {
    	
    	switch(v.getId()) {
    	case R.id.cameraButton:
    		Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
            break;
            
    	case R.id.photoGalleryButton:
    		Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
    		photoPickerIntent.setType("image/*");
    		startActivityForResult(photoPickerIntent, SELECT_PHOTO);
    		break;
    		
    	case R.id.toProfileButton:
    		Intent myIntent = new Intent(v.getContext(), UserProfile.class);
		    v.getContext().startActivity(myIntent);
    		break;
    		
    	/*case R.id.viewFriendsButton:
    		Intent friendsIntent = new Intent(v.getContext(), FriendsActivity.class);
		    v.getContext().startActivity(friendsIntent);
    		break;*/
    		
    	case R.id.addFriendButton:
    		Intent addFriendIntent = new Intent(v.getContext(), AddFriendActivity.class);
		    v.getContext().startActivity(addFriendIntent);
    		break;
    		
    	case R.id.photoStreamButton:
    		Intent photoStreamIntent = new Intent(v.getContext(), PhotoStreamActivity.class);
		    v.getContext().startActivity(photoStreamIntent);
    		break;
    		
    	/*case R.id.profileButton:
    		setContentView(R.layout.profile);
    		TextView name   = (TextView)findViewById(R.id.nameField);
    	    TextView email   = (TextView)findViewById(R.id.emailField);
    	    TextView dateJoined   = (TextView)findViewById(R.id.dateJoinedField);
    		
    	    name.setText("Jason Yim");
    	    email.setText("jyimmer@gmail.com");
    	    dateJoined.setText("September 26, 2011");
    		break;*/
    	}
    }
    
    /** Process the result of activities */
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    	if(resultCode == RESULT_OK)
    	{
    		Intent uploadIntent = new Intent(this, UploadActivity.class);
	    	switch(requestCode) {
	    	case CAMERA_PIC_REQUEST:
	            photo = (Bitmap) data.getExtras().get("data");
	             
	            uploadIntent.putExtra("BitmapImage", photo);
			    startActivity(uploadIntent);
	            break;
	    	case SELECT_PHOTO:
	    		Uri selectedImage = data.getData();
	            InputStream imageStream;
				try {
					imageStream = getContentResolver().openInputStream(selectedImage);
					photo = BitmapFactory.decodeStream(imageStream);
					
		            uploadIntent.putExtra("BitmapImage", photo);
				    startActivity(uploadIntent);
		            
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				}
	            break;
	    	}
    	}
    }
    
    @Override
    protected void onResume() {
    	super.onResume();
    }
    
}